package com.ktgj.customer.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.MallProductSupplier;
import com.ktgj.customer.service.IMallProductSupplierService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 商城商品供应商Controller
 * 
 * @author ruoyi
 * @date 2022-05-30
 */
@RestController
@RequestMapping("/MallProductSupplier")
public class MallProductSupplierController extends BaseController
{
    @Autowired
    private IMallProductSupplierService mallProductSupplierService;

    /**
     * 查询商城商品供应商列表
     */
    @PreAuthorize(hasPermi = "customer:supplier:list")
    @GetMapping("/list")
    public TableDataInfo list(MallProductSupplier mallProductSupplier)
    {
        startPage();
        List<MallProductSupplier> list = mallProductSupplierService.selectMallProductSupplierList(mallProductSupplier);
        return getDataTable(list);
    }

    /**
     * 供应商列表
     * @return
     */
    @PostMapping("/mallProductList")
    public AjaxResult mallProductList() {
        List<MallProductSupplier> list = mallProductSupplierService.selectMallProductSupplierList(null);
        List<Map<String, String>> mapList = new ArrayList<>();
        list.forEach(item -> {
            Map<String, String> map = new HashMap<>();
            map.put("id", String.valueOf(item.getSupplierId()));
            map.put("supplierName", item.getSupplierName());
            mapList.add(map);
        });
        return AjaxResult.success(mapList);
    }

    /**
     * 导出商城商品供应商列表
     */
    @PreAuthorize(hasPermi = "customer:supplier:export")
    @Log(title = "商城商品供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallProductSupplier mallProductSupplier) throws IOException
    {
        List<MallProductSupplier> list = mallProductSupplierService.selectMallProductSupplierList(mallProductSupplier);
        ExcelUtil<MallProductSupplier> util = new ExcelUtil<MallProductSupplier>(MallProductSupplier.class);
        util.exportExcel(response, list, "商城商品供应商数据");
    }

    /**
     * 获取商城商品供应商详细信息
     */
    @PreAuthorize(hasPermi = "customer:supplier:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mallProductSupplierService.selectMallProductSupplierById(id));
    }

    /**
     * 新增商城商品供应商
     */
    @PreAuthorize(hasPermi = "customer:supplier:add")
    @Log(title = "商城商品供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallProductSupplier mallProductSupplier)
    {
        return toAjax(mallProductSupplierService.insertMallProductSupplier(mallProductSupplier));
    }

    /**
     * 修改商城商品供应商
     */
    @PreAuthorize(hasPermi = "customer:supplier:edit")
    @Log(title = "商城商品供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallProductSupplier mallProductSupplier)
    {
        return toAjax(mallProductSupplierService.updateMallProductSupplier(mallProductSupplier));
    }

    /**
     * 删除商城商品供应商
     */
    @PreAuthorize(hasPermi = "customer:supplier:remove")
    @Log(title = "商城商品供应商", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mallProductSupplierService.deleteMallProductSupplierByIds(ids));
    }
}
